package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import jxl.Cell;
import jxl.JXLException;
import jxl.Sheet;
import jxl.Workbook;

import com.model.Clazz;
import com.model.ClazzProanswer;
import com.model.Paper;
import com.model.Student;
import com.model.User;
import com.service.ClazzService;
import com.service.PaperService;
import com.service.TeacherService;
import com.util.BaseAction;
import com.util.Pagination;

public class ClazzAction extends BaseAction {
    public List getDatalist() {
        return datalist;
    }

    public void setDatalist(List datalist) {
        this.datalist = datalist;
    }

    public Integer getUid() {
        return uid;
    }

    public Integer getCurrentpage() {
        return currentpage;
    }

    public Integer getPagesize() {
        return pagesize;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public void setCurrentpage(Integer currentpage) {
        this.currentpage = currentpage;
    }

    public void setPagesize(Integer pagesize) {
        this.pagesize = pagesize;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    private static final long serialVersionUID = -7100734244743577299L;

    public PaperService getPaperService() {
        return paperService;
    }

    @Resource(name = "PaperService")
    public void setPaperService(PaperService paperService) {
        this.paperService = paperService;
    }

    public Pagination getPaperPage() {
        return paperPage;
    }

    public void setPaperPage(Pagination paperPage) {
        this.paperPage = paperPage;
    }

    public ClazzProanswer getProanswer() {
        return proanswer;
    }

    public void setProanswer(ClazzProanswer proanswer) {
        this.proanswer = proanswer;
    }

    public String getClassnum() {
        return classnum;
    }

    public void setClassnum(String classnum) {
        this.classnum = classnum;
    }

    public String getClassname() {
        return classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    public TeacherService getTeacherService() {
        return teacherService;
    }

    @Resource(name = "TeacherService")
    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public List<Paper> getPaperlist() {
        return paperlist;
    }

    public void setPaperlist(List<Paper> paperlist) {
        this.paperlist = paperlist;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private Clazz clazz;

    private ClazzService clazzService;

    private PaperService paperService;

    private List<Clazz> clazzList;

    private List<Student> stuList;

    private List<Paper> paperlist;

    private List datalist;

    private Paper paper;

    private Integer num;

    private ClazzProanswer proanswer;

    private TeacherService teacherService;

    private String info;

    private Pagination paperPage;

    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    private Integer id;
    private Integer uid;
    private String username;
    private String password;
    private Student student;
    private String classnum;
    private String classname;
    private Integer currentpage;
    private Integer pagesize;

    public ClazzService getClazzService() {
        return clazzService;
    }

    @Resource(name = "ClazzService")
    public void setClazzService(ClazzService clazzService) {
        this.clazzService = clazzService;
    }

    public Clazz getClazz() {
        return clazz;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    public List<Clazz> getClazzList() {
        return clazzList;
    }

    public void setClazzList(List<Clazz> clazzList) {
        this.clazzList = clazzList;
    }

    public List<Student> getStuList() {
        return stuList;
    }

    public void setStuList(List<Student> stuList) {
        this.stuList = stuList;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String ClazzList() {

        if (clazz == null) {
            clazz = new Clazz();
        }

        clazzList = clazzService.ClazzList(clazz);

        return "clazzList";
    }

    public String StuList() {

        stuList = clazzService.getStudenListByClazzid(id);
        clazz = clazzService.findByClazzId(id);
        return "StuList";
    }

    public String addClazz() {
        clazz = new Clazz();
        clazz.setClassname(classname);
        int nclassnum = Integer.parseInt(classnum);
        clazz.setClassnum(nclassnum);
        clazzService.addClazz(clazz);
        return "toClazzList";
    }

    public String ImportStu() throws IOException, JXLException {
        File file = getUpload();
        List<Student> l = new ArrayList<Student>();
        if (clazz == null) {
            clazz = new Clazz();
        }
        clazz = clazzService.findByclassnum(clazz.getClassnum());

        if (clazz == null) {
            info = "该班级不存在！";
            return "error";
        }

        // 打开文件
        FileInputStream fis = new FileInputStream(file);
        Workbook book = Workbook.getWorkbook(fis);
        fis.close();

        // 获得第一个工作表对象
        Sheet sheet = book.getSheet(0);

        if (sheet.getColumns() != 8) {
            info = "模板格式不正确，请重新导入一个正确的模板！";
            return "error";
        }

        // 获取总行数
        int rows = sheet.getRows();
        for (int r = 2; r < rows; r++) {
            Cell[] cels = sheet.getRow(r);
            if (cels == null || cels.equals("")) {
                break;
            }
            if ((cels[0].getContents() == null || cels[0].getContents().equals("")) && (cels[1].getContents() == null || cels[1].getContents().equals(""))) {
                break;
            }
            if (cels[0].getContents() == null || cels[0].getContents().equals("")) {
                info = "学号不能为空！";
                return "error";
            }
            Student stu = new Student();
            stu.setClazz(clazz);
            stu.setUsername(cels[0].getContents());
            stu.setPassword(cels[0].getContents());
            stu.setStuname(cels[1].getContents());
            l.add(stu);
        }
        clazzService.addStuList(l);
        return "success";
    }

    public String addpaperclazz() {
        if (paper == null) {
            paper = new Paper();
        }
        paper.setIsvisible("0");

        paper.setPaperpw(null);

        paper.setRank("0");

        User u = (User) session.get("loginUser");
        paper.setUser(u);
        teacherService.addPaper(paper);
        session.put("paperid", paper.getId());
        return "toClazzList";
    }

    public String Login() {

        Student stu = new Student();
        stu.setUsername(username);
        List<Student> list = new ArrayList<Student>();
        list = clazzService.findByExample(stu);
        if (list.size() == 0) {
            info = "改用户还没注册！";
            return "login";
        } else {
            stu.setPassword(password);
            list = clazzService.findByExample(stu);
            if (list.size() == 0) {
                info = "密码错误!";
                return "login";
            }
        }
        Student student = new Student();
        student = clazzService.findByExample(stu).get(0);
        session.put("student", student);

        paperlist = clazzService.getStudentPapers(student.getClazz().getId());
        session.put("paperlist", paperlist);

        List<Clazz> clazzlist = clazzService.getAllClazzList(student.getUsername());
        session.put("classlist", clazzlist);
        
        String isclass = "yes";
        session.put("clazzid", student.getClazz().getId());
        session.put("role", "0");
        session.put("isclass", isclass);
        return "index";
        
    }
    
    public String selectPaperByClazz(){
    	
    	
    	paperlist = clazzService.getStudentPapers(Integer.parseInt(info));
    	session.put("clazzid", info);
        session.put("paperlist", paperlist);
     
        Student stu = (Student) session.get("student");
        
        stu = clazzService.getStu(stu.getUsername(), info);
        
        session.put("student", stu);
        
        List<Clazz> clazzlist = clazzService.getAllClazzList(stu.getUsername());
        session.put("classlist", clazzlist);
    	return "right";
    }

    public String showScore() {
        Student stu = (Student) session.get("student");
        int sid = stu.getId();
        datalist = clazzService.showScore(sid);
        return "showscore";
    }

    public String info() {
    	Student stu = (Student) session.get("student");
    	String clazzid = session.get("clazzid")+"";
        student = clazzService.getStu(stu.getUsername(), clazzid);
        return "info";
    }

    public String editInfo() {
    	Student stu = (Student) session.get("student");
    	String clazzid = session.get("clazzid")+"";
        student = clazzService.getStu(stu.getUsername(), clazzid);
        return "editinfo";
    }

    public String toInfo() {
       
        String pas = student.getPassword().trim(); // 去除字符串前面空格
        if (pas.equals("")) {
            info = "密码不可为空";
            return "backEdit";
        } else {
            student = clazzService.editInfo(student.getId(),student.getUsername(), pas);
            session.put("student", student); // 修改信息后
                                             // 更新session值
            return "infot";
        }
    }

    public void checkNum() throws IOException {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        out = response.getWriter();
        if (!clazzService.checkNum(num)) {
            out.print("该班级不存在");
        }
    }

    public String StuInfo() {
        student = clazzService.StuInfo(id);
        return "StuInfo";
    }

    public String teaToEditUInfo() {
        int sid = student.getId();

        String pas = student.getPassword().trim(); // 去除字符串前面空格
        String stuname = student.getStuname().trim();
        if (pas.equals("") || stuname.equals("")) {
            info = "密码或姓名不可为空";
            id = sid;
            return "backStuInfo";
        } else {

        	student = clazzService.editInfo(student.getId(),student.getUsername(), pas);
            id = student.getClazz().getId();
            
            return "toStuList";
        }

    }

    public String addStudent() {
        clazzService.addStudent(student);
        id = student.getClazz().getId();
        return "addStuok";
    }

    public void delStudent() {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        try {
            clazzService.delStudent(id);
            out = response.getWriter();
            out.print("删除成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void delClazz() {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        try {
            clazzService.delClazzById(id);
            out = response.getWriter();
            out.print("删除成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void checkname() {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            Student stu = new Student();
            stu.setUsername(username);
            List<Student> list = new ArrayList<Student>();
            list = clazzService.checkNameExitByClazzid(stu, id);
            if (list.size() == 0) {
                out.print("1%该用户名可用");
            } else {
                out.print("0%该用户名不可用");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public String StuScore() {
        currentpage = (currentpage == null ? 1 : currentpage);
        pagesize = (pagesize == null ? 30 : pagesize);
        if (paperPage == null) {
            paperPage = new Pagination(1, 30);
        }

        if (currentpage < 0) {
            currentpage = 1;
        }
        if (currentpage > paperPage.getTotalpagecount() && paperPage.getTotalpagecount() != 0) {
            currentpage = (int) paperPage.getTotalpagecount();
        }
        paperPage.setCurrentpage(currentpage);
        paperPage.setPagesize(pagesize);

        if (proanswer == null) {
            proanswer = new ClazzProanswer();
            proanswer.setPaper(new Paper());
            proanswer.setStudent(new Student());
        }

        paperPage = clazzService.StuScore(proanswer, paperPage, id);
        return "StuScore";

    }

    public String paperInfo() {
        int pid = id;
        datalist = clazzService.paperInfo(uid, pid);
        return "paperInfo";
    }

    public String getPaperBycid() {
        if (id != null) {
            datalist = clazzService.getPaperBycid(id);
        }
        return "getPaperByclass";
    }

}